package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQuery;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private ITagService tagService;
    @Autowired
    private IStrategyCommentService strategyCommentService;

    /**
     * 1.根据从前台接收的攻略id查询其下的所有评论
     * 中包含的标签
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/tags")
    public List<Tag> getTags(@PathVariable Long id) {
        return tagService.listByStrategyId(id);
    }

    /**
     * 2.根据从前台接收的攻略id查询其下的所有评论
     *
     * @param qo
     * @param id
     * @return
     */
    @GetMapping("/{id}/comments")
    public PageInfo query(StrategyCommentQueryObject qo, @PathVariable Long id) {
        qo.setStrategyId(id);
        qo.setOrderBy("sc.createTime desc");
        return strategyCommentService.query(qo);
    }
    @GetMapping
    public PageInfo queryAllStrategy(QueryObject qo) {
        return strategyDetailService.query(qo);
    }

    /**
     * 3.保存用户提交的攻略评论
     * 并且保存标签到标签表,维护攻略评论和标签的中间表
     *
     * @param strategyComment
     * @param tags
     * @return
     */
    @PostMapping("/{id}/comments")
    public JSONResult saveComments( StrategyComment strategyComment, String[] tags) {
        JSONResult jsonResult = new JSONResult();
        Long commentId = null;
        try {
            strategyComment.setUser(UserContext.getCurrentUser());
            commentId = strategyCommentService.save(strategyComment, tags);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("保存失败");
        }
        return jsonResult.setResult(commentId);
    }

    /**
     * 4.根据攻略评论的id查询某一篇攻略评论
     *
     * @param id
     * @return
     */
    @GetMapping("/comments/{id}")
    public StrategyComment getCommentById(@PathVariable Long id) {
        return strategyCommentService.getCommentById(id);
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public PageInfo listStrategies(@ModelAttribute("qo") StrategyQuery qo) {
        return strategyService.query(qo);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Strategy getById(@PathVariable Long id) {
        return strategyService.getById(id);
    }

    @RequestMapping(value = "/{id}/catalogs", method = RequestMethod.GET)
    public List<StrategyCatalog> listCatalogsByStrategyId(@PathVariable Long id) {
        return strategyCatalogService.allByStrategyId(id);
    }

    @RequestMapping(value = "/details/{id}", method = RequestMethod.GET)
    public StrategyDetail listDetailByCatalogsId(@PathVariable Long id) {
        return strategyDetailService.getDetailId(id);
    }

    //查询大家都在看的游记
    @RequestMapping(value = "/{id}/hots",method = RequestMethod.GET)
    public List<Travel> listTravelByStrategyId(@PathVariable Long id){
        return travelService.listTravelByStrategyId(id);
    }

    //根据用户的id来查询他所有的攻略评论
    @GetMapping("/users/{userId}/comments")
    public PageInfo listStrategyCommentByUserId(StrategyCommentQueryObject qo){
        qo.setOrderBy("sc.commendTime desc");
       return strategyCommentService.query(qo);
    }

}
